﻿Imports Control_Vuelos_BL
Imports DevExpress.XtraGrid.Views.Grid

Public Class frmSeg_Usuario
    Inherits frmBaseMantenimiento

    Private ReadOnly oUsuario As New UsuarioBL()
    Private ReadOnly oMaestro As New MaestrosBL()

    Private Sub frmSeg_Usuario_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        AddHandler BarBtnRefresh.ItemClick, AddressOf prl_Refresh
        AddHandler UcMantUsuario1.GridView1.FocusedRowChanged, AddressOf prl_FocusedRowChanged
        AddHandler BarBtnAgregar.ItemClick, AddressOf prl_nuevo
        AddHandler BarBtnModificar.ItemClick, AddressOf prl_Editar
        AddHandler UcMantUsuario1.BarManager1.ItemClick, AddressOf prl_BarItemClick
        UcMantUsuario1.cbcodnive.Properties.DataSource = oMaestro.pr_GetPerfiles()
        prl_Refresh()
    End Sub

    Private Sub prl_BarItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
        Dim grid As GridView = CType(UcMantUsuario1.GridView1, GridView)
        If grid.RowCount > 0 Then
            Dim row As DataRow = TryCast(grid.GetDataRow(grid.GetSelectedRows()(0)), DataRow)
            If Not row Is Nothing Then
                If e.Item.Caption = "Restablecer contraseña" Then
                    Using sfrmChangePsw As New frmSegCambiarContrasena() With {.m_coduser = row("coduser")}
                        sfrmChangePsw.ShowDialog()
                    End Using
                End If
                If e.Item.Caption = "Deshabilitar usuario" Then
                    Dim oDialogResult As System.Windows.Forms.DialogResult
                    Using sfrmChk As New frmChk_ArriboMessage() With {.Text = " Deshabilitar usuario "}
                        sfrmChk.lbMensaje.Text = " Desea deshabilitar el usuario ? "
                        sfrmChk.npMain.Text = String.Format(" Se procedera a confirmar la deshabilitación del usuario  {0}{1}", Constants.vbCrLf, row("nomuser"))
                        oDialogResult = sfrmChk.ShowDialog()
                        If oDialogResult = System.Windows.Forms.DialogResult.OK Then
                            If oUsuario.pr_ActivarDesactivarUsuario(row("coduser"), "I") Then
                                prl_Refresh()
                            End If
                        End If
                    End Using

                End If

                If e.Item.Caption = "Habilitar usuario" Then
                    Dim oDialogResult As System.Windows.Forms.DialogResult
                    Using sfrmChk As New frmChk_ArriboMessage() With {.Text = " Habilitar usuario "}
                        sfrmChk.lbMensaje.Text = " Desea Habilitar al usuario ? "
                        sfrmChk.npMain.Text = String.Format(" Se procedera a confirmar la habilitación del usuario  {0}{1}", Constants.vbCrLf, row("nomuser"))
                        oDialogResult = sfrmChk.ShowDialog()
                        If oDialogResult = System.Windows.Forms.DialogResult.OK Then
                            If oUsuario.pr_ActivarDesactivarUsuario(row("coduser"), "A") Then
                                prl_Refresh()
                            End If
                        End If
                    End Using
                End If
            End If
        End If
    End Sub

    Private Sub prl_Refresh()
        UcMantUsuario1.GridControl1.DataSource = oUsuario.pr_Select_Usuarios("")
    End Sub

    Private Sub prl_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs)
        Dim grid As GridView = CType(sender, GridView)
        If grid.RowCount > 0 Then
            Dim row As DataRow = TryCast(grid.GetDataRow(grid.GetSelectedRows()(0)), DataRow)
            If Not row Is Nothing Then
                prl_FocusedFila(row)
            End If
        End If
    End Sub

    Private Sub prl_FocusedFila(p_row As DataRow)
        UcMantUsuario1.gcoduser.EditValue = p_row("coduser")
        UcMantUsuario1.gnomuser.EditValue = p_row("nomuser")
        UcMantUsuario1.cbcodnive.EditValue = p_row("codnive")
        UcMantUsuario1.gfchingr.EditValue = p_row("fchingr")
        UcMantUsuario1.gfchterm.EditValue = p_row("fchterm")
        UcMantUsuario1.gmaiuser.EditValue = p_row("maiuser")
        UcMantUsuario1.chkflgLock.EditValue = p_row("flglock")
        UcMantUsuario1.chkflgLgn.EditValue = p_row("flglgn")
    End Sub

    Private Sub prl_nuevo()
        Dim oDialogResult As System.Windows.Forms.DialogResult
        Using sfrmMantUser As New frmSeg_UsuarioMantenimiento() With {.Text = "Usuarios (AGREGAR)"}
            oDialogResult = sfrmMantUser.ShowDialog()
            If oDialogResult = Windows.Forms.DialogResult.OK Then
                prl_Refresh()
            End If
        End Using
    End Sub

    Private Sub prl_Editar()
        Dim oDialogResult As System.Windows.Forms.DialogResult
        Using sfrmMantEdi As New frmSeg_UsuarioEdicion() With {.Text = "Usuarios (EDITAR)"}
            sfrmMantEdi.UcMantUsuario21.cbcodnive.Properties.DataSource = UcMantUsuario1.cbcodnive.Properties.DataSource
            sfrmMantEdi.UcMantUsuario21.gcoduser.EditValue = UcMantUsuario1.gcoduser.EditValue
            sfrmMantEdi.UcMantUsuario21.gnomuser.EditValue = UcMantUsuario1.gnomuser.EditValue
            sfrmMantEdi.UcMantUsuario21.gfchingr.EditValue = UcMantUsuario1.gfchingr.EditValue
            sfrmMantEdi.UcMantUsuario21.gfchterm.EditValue = UcMantUsuario1.gfchterm.EditValue
            sfrmMantEdi.UcMantUsuario21.gmaiuser.EditValue = UcMantUsuario1.gmaiuser.EditValue
            sfrmMantEdi.UcMantUsuario21.cbcodnive.EditValue = UcMantUsuario1.cbcodnive.EditValue
            sfrmMantEdi.UcMantUsuario21.chkflgLock.EditValue = UcMantUsuario1.chkflgLock.EditValue
            sfrmMantEdi.UcMantUsuario21.chkflgLgn.EditValue = UcMantUsuario1.chkflgLgn.EditValue
            oDialogResult = sfrmMantEdi.ShowDialog
            If oDialogResult = Windows.Forms.DialogResult.OK Then
                prl_Refresh()
            End If
        End Using
    End Sub

End Class